#include "gateway.h"


void sendGetRequest(char* url,size_t (*fn)(void *contents, size_t size, size_t nmemb, void *userp)){
     //初始化
    CURL *curl=  curl_easy_init();
    if (curl)
    {
        //设置请求地址
        curl_easy_setopt(curl,CURLOPT_URL,url);
        //设置回调函数
        curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,fn);
        //发送请求
        CURLcode code = curl_easy_perform(curl);
         //判断是否发送成功
        if (code!=CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(code));
        }
        //
        curl_easy_cleanup(curl);

    }
}

void sendPostRequest(char* url,char*parmStr,size_t (*fn)(void *contents, size_t size, size_t nmemb, void *userp)){
     //初始化
    CURL *curl=  curl_easy_init();
    if (curl)
    {
        //设置请求地址
        curl_easy_setopt(curl,CURLOPT_URL,url);
         //设置post参数
        curl_easy_setopt(curl,CURLOPT_POSTFIELDS,parmStr);
        //设置回调函数
        curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,fn);
        //发送请求
        CURLcode code = curl_easy_perform(curl);
         //判断是否发送成功
        if (code!=CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(code));
        }
        //
        curl_easy_cleanup(curl);
    }
}

int downloadRes(char *url,char * savleFilePath){
    //要保持的文件路径
    FILE *saveFile =  fopen(savleFilePath,"wb");
    //初始化
    CURL *curl = curl_easy_init();
    if (curl)
    {
        //设置相关参数  url  存数据的文件
        curl_easy_setopt(curl,CURLOPT_URL,url);   
        //设置访问资源之间存放文件
        curl_easy_setopt(curl,CURLOPT_FILE,saveFile);  
        
        //发请求
         CURLcode res = curl_easy_perform(curl);

        //判断是否发送成功
        if (res!=CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
            return -1;
        }
        curl_easy_cleanup(curl);

    }
    
    return 0;
}
